package com.example.boardnotifytest_szx305;

/**
 * Created by 单不闲 on 2017/6/1.
 */
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.NotificationCompat;
import android.widget.Toast;


public class MyReceiver extends BroadcastReceiver {
    private Context context;//上下文
    private String str="";//接收到广播的数据


    public static NotificationManager manager;
    public static int notifyID=1;
    @Override
    public void onReceive(Context context, Intent intent) {
        this.context=context;
        str=intent.getStringExtra("para2");
        Toast.makeText(context,"接收到广播："+str,Toast.LENGTH_SHORT).show();

        sendnotify();//发出通知
    }

    private void sendnotify() {
        //获取manager
        manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        //实例化builder
        NotificationCompat.Builder builder=new NotificationCompat.Builder(context);
        //配置builder
        //点击通知后打开新窗口
        Intent intent=new Intent(context,NotifyActivity.class);
        intent.putExtra("para3",str);
        PendingIntent pintent=PendingIntent.getActivity(context,notifyID,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        //配置默认必选的属性
        builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("通知的标题")
                .setContentText("通知的内容"+str)
                .setContentIntent(pintent)
                .setTicker("通知来了")
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis());
        //发送通知
        manager.notify(notifyID,builder.build());
    }
}
